package net.bithappens.hnqe.scratchpad.experiment;

import java.net.URI;
import java.util.Collection;
import java.util.Set;

import net.bithappens.hnqe.graph.GraphNode;
import net.bithappens.hnqe.impl.HNQE;
import net.bithappens.hnqe.query.Instance;
import net.bithappens.hnqe.query.Node;
import net.bithappens.hnqe.query.QueryFactory;
import net.bithappens.hnqe.query.QueryTree;
import net.bithappens.hnqe.query.Relation;
import net.bithappens.hnqe.query.ValueNode;
import net.bithappesn.hnqe.traverse.GraphPath;

public class PerformExperiment {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {

		HNQE hnqe = new HNQE();
		
		hnqe.startFrameWork();
		
		Thread.sleep(40000L);

		
		QueryTree query = null;
		QueryFactory factory = new QueryFactory();
		
		long time;
		
		
//		Node person = factory.newNode("Person");
//		query = QueryFactory.OR(
//			QueryFactory.EQUAL(person.getProperty("name"), QueryFactory.newConstant("person_name_85")),
//			QueryFactory.GREATHEREQUAL(person.getProperty("costPerHour"), QueryFactory.newConstant(18.0)),
//			QueryFactory.GREATHEREQUAL(person.getProperty("employment"), QueryFactory.newConstant("admin"))
//		);
//				
//		time = System.currentTimeMillis();
//		Set<Instance> res = hnqe.getQueryInterface().queryGraph("experiment", 0, query);
//		System.out.println("Query1: "+ (System.currentTimeMillis() - time));
//		System.out.println("Query1 res size: "+ res.size() + "\n\n");
//		
//		
//		
//		
//		Node service = factory.newNode("Service");
//		query = QueryFactory.AND(
//			QueryFactory.NOT(QueryFactory.EQUAL(service.getProperty("status"), QueryFactory.newConstant("running"))),
//			QueryFactory.NOT(QueryFactory.EQUAL(service.getProperty("name"), QueryFactory.newConstant("service_name_1")))
//		);
//		
//		time = System.currentTimeMillis();
//		res = hnqe.getQueryInterface().queryGraph("experiment", 0, query);
//		System.out.println("Query2: "+ (System.currentTimeMillis() - time));
//		System.out.println("Query2 res size: "+ res.size() + "\n\n");
//		
//		
//		
//		
//		Node container = factory.newNode("Container");
//		Relation isDeployed = factory.newRelation("isDeployedAtWithId");
//		query = QueryFactory.AND(
//			QueryFactory.EQUAL(service.getProperty("name"), QueryFactory.newConstant("service_name_85")),
//			QueryFactory.ANYCONNECTION(isDeployed, service, container)
//		);
//		
//		time = System.currentTimeMillis();
//		res = hnqe.getQueryInterface().queryGraph("experiment", 0, query);
//		System.out.println("Query3: "+ (System.currentTimeMillis() - time));
//		System.out.println("Query3 res size: "+ res.size() + "\n\n");
//		
//		
//		
//		
//		
//		ValueNode deployId = factory.newValueNode(35);
//		query = QueryFactory.AND(
//			QueryFactory.EQUAL(service.getProperty("name"), QueryFactory.newConstant("service_name_85")),
//			QueryFactory.EXACTMATCH(isDeployed, service, container, deployId)
//		);
//					
//		time = System.currentTimeMillis();
//		res = hnqe.getQueryInterface().queryGraph("experiment", 0, query);
//		System.out.println("Query4: "+ (System.currentTimeMillis() - time));
//		System.out.println("Query4 res size: "+ res.size() + "\n\n");
//		
//		
//		
//		
//		
//		
//		Node scu = factory.newNode("SCU");
//		Relation maintains = factory.newRelation("maintains");
//		query = QueryFactory.AND(
//			QueryFactory.EQUAL(service.getProperty("status"), QueryFactory.newConstant("error")),
//			QueryFactory.ANYCONNECTION(isDeployed, service, container),
//			QueryFactory.OR(
//				QueryFactory.EXACTMATCH(maintains, person, container),
//				QueryFactory.EXACTMATCH(maintains, scu, container)
//			)
//		);
//
//		time = System.currentTimeMillis();
//		res = hnqe.getQueryInterface().queryGraph("experiment", 0, query);
//		System.out.println("Query5: "+ (System.currentTimeMillis() - time));
//		System.out.println("Query5 res size: "+ res.size() + "\n\n");
		
		
		
		
		
		
		
		
		
		
		
		
		GraphNode start = new GraphNode("Service", new URI("http://sose.com/node/service_0"), null);
		GraphNode end = new GraphNode("Service", new URI("http://sose.com/node/service_499"), null);
		
		time = System.currentTimeMillis();
		Collection<GraphPath> paths = hnqe.getCachTraverseInterface().findPath(
				"experiment", start, end, 3, true, "SimpleTypeValidator", 
				"Service", "Container", "SCU", "Person", "maintains", "isDeployedAtWithId", "workForSCU", "hasSkill");
		
		System.out.println("Path1: "+ (System.currentTimeMillis() - time));
		if(paths != null){
			System.out.println("Path1 res size: "+ paths.size() + "\n\n");
		}
		
		
		
		
		
		
		
		time = System.currentTimeMillis();
		paths = hnqe.getCachTraverseInterface().findPath(
				"experiment", start, end, 4, true, "SimpleTypeValidator", 
				"Service", "Container", "SCU", "Person", "maintains", "isDeployedAtWithId", "workForSCU", "hasSkill");
		
		System.out.println("Path2: "+ (System.currentTimeMillis() - time));
		if(paths != null){
			System.out.println("Path2 res size: "+ paths.size() + "\n\n");
		}
		
		
		
		
		
		
		
		time = System.currentTimeMillis();
		paths = hnqe.getCachTraverseInterface().findPath(
				"experiment", start, end, 5, true, "SimpleTypeValidator", 
				"Service", "Container", "SCU", "Person", "maintains", "isDeployedAtWithId", "workForSCU", "hasSkill");
		
		System.out.println("Path3: "+ (System.currentTimeMillis() - time));
		if(paths != null){
			System.out.println("Path3 res size: "+ paths.size() + "\n\n");
		}
		
		
		
		
		
		
		
		time = System.currentTimeMillis();
		paths = hnqe.getCachTraverseInterface().findPath(
				"experiment", start, end, 6, true, "SimpleTypeValidator", 
				"Service", "Container", "SCU", "Person", "maintains", "isDeployedAtWithId", "workForSCU", "hasSkill");
		
		System.out.println("Path4: "+ (System.currentTimeMillis() - time));
		if(paths != null){
			System.out.println("Path4 res size: "+ paths.size() + "\n\n");
		}
		
		
		
		
		time = System.currentTimeMillis();
		paths = hnqe.getCachTraverseInterface().findPath(
				"experiment", start, end, 7, true, "SimpleTypeValidator", 
				"Service", "Container", "SCU", "Person", "maintains", "isDeployedAtWithId", "workForSCU", "hasSkill");
		
		System.out.println("Path5: "+ (System.currentTimeMillis() - time));
		if(paths != null){
			System.out.println("Path5 res size: "+ paths.size() + "\n\n");
		}
		
		
		
		
		hnqe.stopFrameWork();
	}

}
